package controllers.caterinaController;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import person.Customer;
import person.Employee;
import person.Guest;
import person.User;
import transaction.Transaction;
import budget.Budget;
import budget.Request;
import budget.Service;
import contract.EmployeeContract;
import contract.EventContract;
import controllers.PermissionList;
import controllers.caterinaController.controllers.BudgetManager;
import controllers.caterinaController.controllers.CustomerManager;
import controllers.caterinaController.controllers.EmployeeContractManager;
import controllers.caterinaController.controllers.EmployeeManager;
import controllers.caterinaController.controllers.EventContractManager;
import controllers.caterinaController.controllers.EventManager;
import controllers.caterinaController.controllers.GuestManager;
import controllers.caterinaController.controllers.LoginManager;
import controllers.caterinaController.controllers.RegistrationManager;
import controllers.caterinaController.controllers.RequestManager;
import controllers.caterinaController.controllers.ServiceManager;
import controllers.dbWrapper.InstallationWrapper;
import controllers.caterinaController.controllers.TransactionManager;
import controllers.interfaces.DataController;
import event.Event;

public class CaterinaController implements DataController {
	
	/*------------------------------Session------------------------------*/
	@Override
	public void login(String userName, String password) throws Exception {
		LoginManager.login(userName, password);
	}
	@Override
	public void logout() throws Exception {
		LoginManager.logout();
	}
	@Override
	public User getCurrentUser() throws Exception {
		return LoginManager.getCurrentLogged();
	}
	@Override
	public void registerUser(User user, String userName, String password) throws Exception {
		RegistrationManager.register(user, userName, password);
	}
	@Override
	public void removeUser(User user) throws Exception {
		RegistrationManager.deleteUser(user);
	}
	@Override
	public PermissionList getPermissions() throws Exception {
		return LoginManager.getPermission();
	}
	
	@Override
	public void createSystemUser() throws Exception {
		InstallationWrapper.insertSystemEmployee();
	}
	@Override
	public void setSystemUser(String userName, String password) throws Exception {
		InstallationWrapper.setSystemLogin(userName, password);
	}
	
	public void modifyUserPassword(String password) throws Exception{
		RegistrationManager.modifyUserPassword(password);
	}
	
	/*----------------------------Request------------------------------*/
	@Override
	public void registerRequest(Request request) throws Exception {
		RequestManager.registerRequest(request);
	}
	@Override
	public Collection<Request> getRequests(String searchTherms) throws Exception {
		User u = this.getCurrentUser();
		if(u instanceof Customer)
			return RequestManager.getRequest((Customer)u);
		else
			return RequestManager.getAllRequest();
	}
	@Override
	public void removeRequest(Request request)throws Exception {
		RequestManager.removeRequest(request);
	}
	@Override
	public Budget acceptRequest(Request request) throws Exception {
		return BudgetManager.generateBudget(request);
	}
	@Override
	public void denyRequest(Request request, String motivation) throws Exception {
		RequestManager.removeRequest(request);
	}
	
	/*-----------------------------Budget-----------------------------*/
	@Override
	public Collection<Budget> getBudgets(String searchTerms) throws Exception {
		return BudgetManager.getBudgetBySearchFilter(searchTerms);
	}
	
	@Override
	public void modifyBudget(Budget budget) throws Exception {
		BudgetManager.modifyBudget(budget);
	}
	@Override
	public EventContract acceptBudget(Budget budget) throws Exception {
		 return EventContractManager.contractStipulation(budget);
	}
	@Override
	public void removeBudget(Budget budget) throws Exception {
		BudgetManager.removeBudget(budget);
	}
	
	/*---------------------------Event Contract------------------------*/
	@Override
	public Collection<EventContract> getEventContracts(String searchTerms) throws Exception {
		return EventContractManager.getEventContractBySearchFilter(searchTerms);
	}
	@Override
	public void acceptEventContract(EventContract contract) throws Exception {
		EventContractManager.modifyEventContract(contract);
	}
	
	public void revertEventContract(EventContract contract) throws Exception{
		EventContractManager.revertEventContract(contract);
	}
	
	/*-----------------------------Service---------------------------*/
	@Override
	public Collection<Service> getServices() throws Exception {
		return ServiceManager.getAllServices();	 
	}
	@Override
	public void addService(Service serv) throws Exception {
		ServiceManager.insertService(serv);
	}
	@Override
	public void modifyService(Service serv) throws Exception {
		ServiceManager.updateService(serv);
	}
	@Override
	public void removeService(Service serv) throws Exception {
		ServiceManager.removeService(serv);
	}
	
	/*-----------------------------Event--------------------------------*/
	@Override
	public Collection<Event> getEvents(String searchTerms) throws Exception {
		return EventManager.getAllEvent();
	}
	@Override
	public void nextState(Event evento) throws Exception {
		evento.changeState();
		EventManager.modifyEvent(evento);
	}
	
	/*------------------------------Employee------------------------------*/


	@Override
	public Collection<Employee> getEmployees() throws Exception {
		return EmployeeManager.getEmployeeList();
	}
	
	@Override
	public void firesEmployee(EmployeeContract employeeCont) throws Exception {
		EmployeeManager.firesEmployee(employeeCont);
	}
	
	/*------------------------------Customer------------------------------*/

	@Override
	public Collection<Customer> getCustomers() throws Exception {
		return CustomerManager.getAllCutomers();
	}
	
	/*------------------------------Guests------------------------------*/

	@Override
	public Collection<Guest> getGuests(Event evt) throws Exception {
		return GuestManager.getAllGuests();
	}
	
	/*------------------------------EmployeeContract------------------------------*/

	@Override
	public Collection<EmployeeContract> getEmployeeContracts() throws Exception {
		return EmployeeContractManager.getAllEmployeeContract();
	}
	@Override
	public void acceptEmployeeContract(EmployeeContract employeeContract) throws Exception {
		EmployeeContractManager.insertEmployeeContract(employeeContract);
	}
	@Override
	

	/*---------------------------------Transactions------------------------------*/

	public ArrayList<Transaction> getTransactions() throws Exception {
		return TransactionManager.getAllTransaction();
		
	}
	@Override
	public Map<Integer, Map<String, Double>> getAllStats() throws Exception {
		return TransactionManager.getAllStats();
	}
	@Override
	public void addTransaction(Transaction transaction) throws Exception {
		TransactionManager.registerTransaction(transaction);
	}

	@Override
	public void payWages() throws Exception {
		TransactionManager.payWages();
	}
	@Override
	public Collection<Transaction> getWagesTransactions() throws Exception {
		return TransactionManager.getWagesTransactions();
	}
}
